package demo3;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    public static void main1(String[] args) {
        convert("PAYPALISHIRING",3);
    }

    //Z字型变换
    public static String convert(String s,int numRows){
        if (numRows == 1)
            return s;
        int jump = (numRows - 1) * 2;
        StringBuffer temp = new StringBuffer();

        for (int i = 0; i < numRows; i++) {
            boolean signal = true; // 标志变量，用于跟踪当前使用哪种增值方式
            for (int j = i; j < s.length();) {
                temp.append(s.charAt(j));
                if (i == 0 || i == numRows - 1) {
                    j += jump;
                } else {
                    if (signal) {
                        j += jump - 2 * i;
                        signal = false;
                    } else {
                        j += 2 * i;
                        signal = true;
                    }
                }
            }
        }
        String result = temp.toString();
        return result;
    }

    //Z字型变换
    public static String convert1(String s, int numRows) {
        char[][] array = new char[numRows][];
        int j = 0;
        //int m = numRows-2;
        int n = 0;
        while (n < s.length()) {
            int m = numRows-2;
            int i = 0;
            for (;n < s.length() && i < numRows; i++) {
                char ch = s.charAt(n++);
                array[i][j] = ch;
            }
            i--;
            while (n < s.length() && m > 0) {
                char ch = s.charAt(n++);
                j++;
                i--;
                array[i][j++] = ch;
                m--;
            }
        }
        System.out.println(array);
        return "adf";
    }

    //两数之和   2
    public int[] twoSum(int[] numbers, int target) {
        int left = 0;
        int right = numbers.length-1;
        int[] array = new int[2];
        while (left < right) {
            if (numbers[left] + numbers[right] > target) {
                right--;
            }else if(numbers[left] + numbers[right] < target){
                left++;
            }else {
                array[0] = left+1;
                array[1] = right+1;
            }
        }
        return array;
    }

    //找到字符串中所有异或词
    public int[] findAnagrams(String s, String p) {
        HashMap<Character,Integer> map1 = new HashMap<>();
        HashMap<Character,Integer> map2 = new HashMap<>();
        System.out.println(map1.equals(map2));
        int[] array = new int[s.length()];
        int count = 0;
        for (int i = 0; i < p.length(); i++) {
            char ch = p.charAt(i);
            if(map1.containsKey(ch)){
                int val = map1.get(ch);
                map1.put(ch,val+1);
            }else {
                map1.put(ch,1);
            }
        }
        int a = 0;
        for(int left=0,right=0;right<s.length();){
            if (a<p.length()) {
                char ch = s.charAt(right);
                if(map2.containsKey(ch)){
                    int val = map2.get(ch);
                    map2.put(ch,val+1);
                }else {
                    map2.put(ch,1);
                }
                right++;
            }else {
                int val = map2.get(s.charAt(left));
                if (val - 1 == 0) {
                    map2.remove(s.charAt(left));
                }else {
                    map2.put(s.charAt(left),val-1);
                }
                char ch = s.charAt(right);
                if(map2.containsKey(ch)){
                    int val1 = map2.get(ch);
                    map2.put(ch,val1+1);
                }else {
                    map2.put(ch,1);
                }
                right++;
                left++;
            }
            if(map1.equals(map2)){
                array[count++] = left;
            }
        }
        return Arrays.copyOf(array,count);
    }


    public ListNode deleteDuplicates(ListNode head) {
        ListNode pcur = head;
        ListNode prev = head;
        while (prev != null) {
            while (pcur.val != prev.val) {
                pcur = pcur.next;
            }
            prev.next = pcur;
            prev = prev.next;
        }
        return head;
    }

    //数组中的极差
    public int smallestRangeII(int[] nums, int k) {
        int n = nums.length;
        Arrays.sort(nums);
        int max = nums[n-1];
        int min = nums[0];
        int ret = max - min;
        for (int i = 0; i < n; i++) {
            int a = nums[i];
            int b = nums[i+1];
            ret = Math.min(ret,Math.max(a+k,max-k)-Math.min(min+k,b-k));
        }
        return ret;
    }
}
